package edu.iss.demo.web.controller;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.text.ChangedCharSetException;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import edu.iss.demo.model.Employee;
import edu.iss.demo.model.LeaveApplication;
import edu.iss.demo.service.LeaveApplicationService;



@Controller
public class EmployeeController 
{
	
	@Autowired
	LeaveApplicationService leaveAppService;
	//String empId = ${loggedEmployee.employeeId};
	

	@RequestMapping(value={"/employee","/employee/Leave-Form"}, method=RequestMethod.GET)
	public ModelAndView addLeaveAppDisplay()
	{
		ModelAndView mav = new ModelAndView("emp_leave","Leave",new LeaveApplication());
		return mav;
	}


	@RequestMapping(value = "/employee/success", method = RequestMethod.POST)
	public ModelAndView addLeaveApp(
			@ModelAttribute @Valid LeaveApplication la,BindingResult result,
			final RedirectAttributes redirectAttributes, WebRequest webRequest)
	{
		String fDate = webRequest.getParameter("fromDate");
		String tDate = webRequest.getParameter("toDate");
		la.getEmployee();
		la.setFromDate(changeDateFormat(fDate));
		la.setToDate(changeDateFormat(tDate));
		la.setStatus("Pending");
		la.setApplyDate(new Date());
		ModelAndView mav = new ModelAndView();
		leaveAppService.create(la);
		mav.setViewName("em_successApplication");
		return mav;
	}
	
	//to use for changing string to date format
	public Date changeDateFormat(String d)
	{
		try
		{

			SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			Date result = sd.parse(d);
			return result;

		}
		catch(ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	
	

	//this method is for viewing all pending leave application
	@RequestMapping(value ="/employee/Leavehistory-Appending", method = RequestMethod.GET)
	public ModelAndView empLHA(HttpServletRequest r)
	{
		Employee e = (Employee) r.getSession().getAttribute("loggedEmployee");
		
			ModelAndView mav = new ModelAndView("emp_appendingLeaveHistory");
			List<LeaveApplication> empLeaveList = leaveAppService.findLeaveAppByStatusNEmpId("Pending",e.getEmployeeId() ); 
			mav.addObject("empLeaveList", empLeaveList);
			return mav;
	}
	
	@RequestMapping(value ="/employee/Leavehistory-Appending/{empid}", method = RequestMethod.GET)
	public ModelAndView showPendingLeave(Model model, @PathVariable String empid, HttpSession session)
	{
		Employee e = (Employee) session.getAttribute("loggedEmployee");
		
		ModelAndView mav = new ModelAndView("emp_appendingLeaveHistory");
		List<LeaveApplication> empLeaveList = leaveAppService.findLeaveAppByStatusNEmpId("Pending",e.getEmployeeId() ); 
		mav.addObject("empLeaveList", empLeaveList);
		return mav;
	}
	
	//this method is for viewing all approved leave application
	@RequestMapping(value = "/employee/Leavehistory-Approved", method = RequestMethod.GET)
	public ModelAndView empRLH(HttpServletRequest r)
	{
		Employee e = (Employee) r.getSession().getAttribute("loggedEmployee");
		ModelAndView mav = new ModelAndView("emp_approvedLeaveHistory");
		List<LeaveApplication> empLeaveList = leaveAppService.findLeaveAppByStatusNEmpId("approved", e.getEmployeeId()); 
		mav.addObject("empLeaveList", empLeaveList);
		return mav;
	}
	
	//this method is for viewing all rejected leave application

	@RequestMapping(value ="/employee/Leavehistory-Reject", method = RequestMethod.GET)
	public ModelAndView empLHR(HttpServletRequest r)
	{
		Employee e = (Employee) r.getSession().getAttribute("loggedEmployee");
		ModelAndView mav = new ModelAndView("emp_rejectLeaveHistory");
		List<LeaveApplication> empLeaveList = leaveAppService.findLeaveAppByStatusNEmpId("reject", e.getEmployeeId()); 
		mav.addObject("empLeaveList", empLeaveList);
		return mav;
	}
	
	
	//this is for loading editing leave application
	@RequestMapping(value ="/employee/editLeaveApp/{id}", method = RequestMethod.GET)
	public ModelAndView getEmpEditLeave(@PathVariable int id)
	{
		ModelAndView mav = new ModelAndView("emp_editLeaveApp");
		LeaveApplication editLeave = leaveAppService.findbyid(id);
		mav.addObject("Leave", editLeave);
		return mav;
	}
	
	@RequestMapping(value ="/employee/editLeaveApp/{id}", method = RequestMethod.POST, params = "cancel")
	public ModelAndView backToLeaveHistory(@PathVariable int id)
	{
		ModelAndView mav = new ModelAndView("redirect:/employee/Leavehistory-Appending.html"); 
		
		return mav;
	}
	
	//this is for saving edited data to the database
	@RequestMapping(value ="/employee/editLeaveApp/{id}", method = RequestMethod.POST, params="save")
	public ModelAndView empEditLeave(@ModelAttribute @Valid LeaveApplication la,
			BindingResult result, @PathVariable int id, final RedirectAttributes redirectAttributes
			, WebRequest webRequest)
	{
		
		la.setLeaveId(id); 	
		//System.out.println("kkkkkkkkkkkkkkkkkkkkkkkkkkkk" + la.getFromDate());
		String fDate = webRequest.getParameter("fromDate");
		String tDate = webRequest.getParameter("toDate");
		la.setFromDate(changeDateFormat(fDate));
		la.setToDate(changeDateFormat(tDate));
		//System.out.println("kkkkkkkkkkkkkkkkkkkkkkkkkkkk" + la.getFromDate());
		leaveAppService.empUpdate(la);	
		ModelAndView mav = new ModelAndView("em_successApplication"); //change for only view name
			
		return mav;
		
	}
	
	@RequestMapping(value ="/emp_appendingLeaveHistory", method = RequestMethod.POST, params="cancel")
	public ModelAndView goback(@ModelAttribute @Valid LeaveApplication la,
			BindingResult result, @PathVariable int id, final RedirectAttributes redirectAttributes
			, WebRequest webRequest)
	{
		
		ModelAndView mav = new ModelAndView("redirect:/emp_appendingLeaveHistory"); 
			
		return mav;
		
	}
	//this method is for viewDateilOfLeaveApp
	@RequestMapping(value = "/employee/viewDetailLeaveApp/{id}", method = RequestMethod.GET)
	public ModelAndView viewDetailOfLeaveApp(@PathVariable int id)
	{
		ModelAndView mav = new ModelAndView("emp_viewDetailOfLeave");
		LeaveApplication detailLeave=leaveAppService.findbyid(id);
		mav.addObject("detailLeave", detailLeave);
		return mav;
	}
	
	
	//Employee Navigation Controller
	/*@RequestMapping(value = "/employee", method = RequestMethod.GET)
	public ModelAndView empView()
	{
		return new ModelAndView("emp_leave");
	}
	
	
	@RequestMapping(value = "/employee/info", method = RequestMethod.GET)
	public ModelAndView empInfo()
	{
		return new ModelAndView("employeeInfo");
	}
	
	
	@RequestMapping(value = "/employee/request-attendance", method = RequestMethod.GET)
	public ModelAndView empRA()
	{
		return new ModelAndView("emp_requestAttendance");
	}
	
	
		@RequestMapping(value="/employee/leaveform", method=RequestMethod.POST,params="save")
	public ModelAndView addLeaveApp(@ModelAttribute LeaveApplication la, ModelMap model, WebRequest webRequest)
	{
		leaveAppService.create(la);
		System.out.print(la.getLeaveType());
		System.out.println(la.getEmployeeComment());
		System.out.println(la.getFromDate());
		
		
	}
		
	*/
	
	
	

	
	
	
}
